package com.xuanyang.dormrepair.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xuanyang.dormrepair.model.entity.Notice;
import com.xuanyang.dormrepair.model.mapper.NoticeMapper;
import com.xuanyang.dormrepair.utils.PageKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * @AUTHOR soft
 * @DATE 2019/5/20 0:52
 * @DESCRIBE
 */
@Slf4j
@Service
public class NoticeService implements IService<Notice> {

    @Autowired
    private NoticeMapper noticeMapper;

    @Transactional
    public boolean add(Notice notice) {
        return noticeMapper.insert(notice) > 0;
    }

    @Transactional
    public boolean delete(Serializable pk) {
        return noticeMapper.deleteById(pk) > 0;
    }

    @Transactional
    public boolean update(Notice notice) {
        return noticeMapper.updateById(notice) > 0;
    }

    @Override
    public Notice byId(Serializable pk) {
        return noticeMapper.selectById(pk);
    }

    @Override
    public IPage<Notice> page(int page, int limit, Notice query) {
        LambdaQueryWrapper<Notice> wrapper = Wrappers.lambdaQuery(query);
        wrapper.orderByDesc(Notice::getId);

        return noticeMapper.selectPage(PageKit.of(page, limit), wrapper);
    }
}
